package com.lemon.smartsafety.ui.activity.safe.nfcandqrcode;

import android.support.v4.app.Fragment;
import android.os.Bundle;

import com.lemon.android.http.HttpUtils;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.BundleKey;
import com.lemon.smartsafety.app.EventTag;
import com.lemon.smartsafety.entity.common.EvidenceDataI;
import com.lemon.smartsafety.entity.event.common.HorizontalEvidenceEvent;
import com.lemon.smartsafety.result.safe.ResultCheckNfc;
import com.lemon.smartsafety.tag.BaseTAG;
import com.lemon.smartsafetynew.ui.activity.base.BaseFragmentActivity;
import com.lemon.smartsafety.ui.adapter.common.HorizontalEvidenceAdapter;
import com.lemon.smartsafety.ui.fragment.safe.code.CodeCheckItemRecordListFragment;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafety.utils.common.LemonFileHelper;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.zhy.http.okhttp.OkHttpUtils;

import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import cn.finalteam.galleryfinal.model.PhotoInfo;

public class CodeInspectionCheckItemRecordActivity extends BaseFragmentActivity {
    private LoggerHelper loggerHelper = new LoggerHelper(CodeInspectionCheckItemRecordActivity.class);

    private CodeCheckItemRecordListFragment mFragment;
    
    private ResultCheckNfc resultCheckNfc;

    /**
     * bundle info
     */


    @Override
    protected void getBundleArgs() {
        super.getBundleArgs();
        if (getIntent().getExtras() != null) {
            resultCheckNfc = (ResultCheckNfc) getIntent().getSerializableExtra(BaseTAG.ResultCheckNfc_ItemDATA);

        }
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_code_inspection_check_item_record;
    }

    @Override
    protected void initViews() {
        super.initViews();
    }

    @Override
    public Fragment getFragment() {
        return getFragment(null);
    }


    public Fragment getFragment(String text) {
        Bundle bundle = new Bundle();
        bundle.putString(BundleKey.KEY_WORD, StringUtils.nullValue(text));
        bundle.putSerializable(BaseTAG.ResultCheckNfc_ItemDATA, resultCheckNfc);
        mFragment = CodeCheckItemRecordListFragment.newInstance();
        mFragment.setArguments(bundle);
        return mFragment;
    }


    @Override
    protected void initTitle() {
        this.setBarTitle("巡查记录");
    }

    /**
     * 查看照片
     *
     * @param event
     */
    @Subscribe
    public void onEventMainThread(HorizontalEvidenceEvent event) {
        List<EvidenceDataI> res = event.getData();
        if (res != null && res.size() != 0) {
            EvidenceDataI data = res.get(event.getPosition());
            if (LemonFileHelper.isImageUrl(data.getEvidenceUrl())) {
                List<PhotoInfo> photos = HorizontalEvidenceAdapter.getPhotoInfoList(event.getData());
                if (photos != null && photos.size() != 0) {
                    UIHelper.showPhotoView(this, photos, event.getPosition());
                }
            } else if (LemonFileHelper.isVoiceUrl(data.getEvidenceUrl())) {
                playVoice(HttpUtils.formatFilesUrl(data.getEvidenceUrl()));
            }
        }
    }


    /****************************************************************************************************************
     * 保护现场
     ****************************************************************************************************************/
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        if (savedInstanceState != null) {
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(EventTag.TAG_INSPECTION_CHECK_ITEM_RECORD);
    }

}
